package dao;

import java.util.List;

import model.Employee;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import view.Home;

public class EmployeeDAO {
	private final SessionFactory sf = util.NewHibernateUtil.getSessionFactory();

	@SuppressWarnings("unchecked")
	public List<Employee> showAllEmployee() {
		try {
			sf.getCurrentSession().beginTransaction();
			return sf.getCurrentSession().createQuery("From Employee order by name").list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Employee> showAllEmployeeByAll(String key) {
		sf.getCurrentSession().beginTransaction();
		List<Employee> employee = sf
				.getCurrentSession()
				.createCriteria(Employee.class)
				.add(Restrictions.or(Restrictions.like("id", "%" + key + "%"),
						Restrictions.or(Restrictions.like("name", "%" + key
								+ "%"), Restrictions.or(Restrictions.like(
								"email", "%" + key + "%"), Restrictions.or(
								Restrictions.like("empPerId", "%" + key + "%"),
								Restrictions.or(
										Restrictions.like("phone", "%" + key
												+ "%"),
										Restrictions.like("address", "%" + key
												+ "%"))))))).list();
		sf.getCurrentSession().getTransaction().commit();
		return employee;
	}

	public Employee searchEmployee(String id) {
		try {
			sf.getCurrentSession().beginTransaction();
			return (Employee) sf.getCurrentSession().get(Employee.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean deleteEmployee(Employee e) {
		try {
			sf.getCurrentSession().beginTransaction();
			sf.getCurrentSession().delete(e);
			sf.getCurrentSession().getTransaction().commit();
			return true;
		} catch (Exception e2) {
			e2.printStackTrace();
			return false;
		}
	}

	public boolean updateEmployee(Employee e) {
		try {
			Session session = sf.getCurrentSession();
			session.beginTransaction();
			session.merge(e);
			session.getTransaction().commit();
			return true;

		} catch (Exception e2) {
			e2.printStackTrace();
			return false;
		}
	}

	public boolean insertEmployee(Employee e) {
		try {
			Session session = sf.getCurrentSession();
			session.beginTransaction();
			session.save(e);
			session.getTransaction().commit();
			return true;
		} catch (Exception em) {
			em.printStackTrace();
			sf.getCurrentSession().getTransaction().rollback();
			return false;
		}
	}
	
	public boolean checkAccount(String username, String password){
		try {
			Session session = sf.getCurrentSession();
			session.beginTransaction();
			Home.setEmployee((Employee) session.get(Employee.class, username));
			session.getTransaction().commit();
			
			if(Home.getEmployee() == null)
				return false;
			return Home.getEmployee().getPassword().equals(password);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
